import { SimpleTimeline } from "core/SimpleTimeline";
import { OverwriteManager } from "./OverwriteManager";
import { TweenCore } from "core/TweenCore";
import { TweenLite } from "./TweenLite";
type int = number;
//class TimelineLite
    
    export  class TimelineLite extends SimpleTimeline
    {
        constructor(arg1: Object=null)
        {
            super(arg1);
            this._endCaps = [null, null];
            this._labels = {};
            this.autoRemoveChildren = Boolean(this.vars.autoRemoveChildren == true);
            _hasUpdate = Boolean(typeof this.vars.onUpdate == "function");
            if (typeof this.vars.tweens === "array") 
            {
                this.insertMultiple(this.vars.tweens, 0, this.vars.align == null ? "normal" : this.vars.align, this.vars.stagger ? Number(this.vars.stagger) : 0);
            }
            return;
        }

        public /* override */ get rawTime(): number
        {
            if (!(this.cachedTotalTime == 0) && !(this.cachedTotalTime == this.cachedTotalDuration)) 
            {
                return this.cachedTotalTime;
            }
            return (this.timeline.rawTime - this.cachedStartTime) * this.cachedTimeScale;
        }

        
        {
            TimelineLite._overwriteMode = OverwriteManager.enabled ? OverwriteManager.mode : OverwriteManager.init(2);
        }

        public /* override */ addChild(arg1: TweenCore): void
        {
            var loc3=null;
            var loc4=NaN;
            if (!arg1.cachedOrphan && arg1.timeline) 
            {
                arg1.timeline.remove(arg1, true);
            }
            arg1.timeline = this;
            if (arg1.gc) 
            {
                arg1.setEnabled(true, true);
            }
            setDirtyCache(true);
            var loc1=this.gc ? this._endCaps[0] : _firstChild;
            var loc2=this.gc ? this._endCaps[1] : _lastChild;
            if (loc2 != null) 
            {
                loc3 = loc2;
                loc4 = arg1.cachedStartTime;
                while (!(loc3 == null) && loc4 <= loc3.cachedStartTime) 
                {
                    loc3 = loc3.prevNode;
                }
                if (loc3 != null) 
                {
                    if (loc3.nextNode) 
                    {
                        loc3.nextNode.prevNode = arg1;
                    }
                    else if (loc3 == loc2) 
                    {
                        loc2 = arg1;
                    }
                    arg1.prevNode = loc3;
                    arg1.nextNode = loc3.nextNode;
                    loc3.nextNode = arg1;
                }
                else 
                {
                    loc1.prevNode = arg1;
                    arg1.nextNode = loc1;
                    arg1.prevNode = null;
                    loc1 = arg1;
                }
            }
            else 
            {
                var loc5;
                loc2 = loc5 = arg1;
                loc1 = loc5;
                arg1.prevNode = loc5 = null;
                arg1.nextNode = loc5;
            }
            arg1.cachedOrphan = false;
            if (this.gc) 
            {
                this._endCaps[0] = loc1;
                this._endCaps[1] = loc2;
            }
            else 
            {
                _firstChild = loc1;
                _lastChild = loc2;
            }
            return;
        }

        public /* override */ remove(arg1: TweenCore, arg2: Boolean=false): void
        {
            if (arg1.cachedOrphan) 
            {
                return;
            }
            if (!arg2) 
            {
                arg1.setEnabled(false, true);
            }
            var loc1=this.gc ? this._endCaps[0] : _firstChild;
            var loc2=this.gc ? this._endCaps[1] : _lastChild;
            if (arg1.nextNode) 
            {
                arg1.nextNode.prevNode = arg1.prevNode;
            }
            else if (loc2 == arg1) 
            {
                loc2 = arg1.prevNode;
            }
            if (arg1.prevNode) 
            {
                arg1.prevNode.nextNode = arg1.nextNode;
            }
            else if (loc1 == arg1) 
            {
                loc1 = arg1.nextNode;
            }
            if (this.gc) 
            {
                this._endCaps[0] = loc1;
                this._endCaps[1] = loc2;
            }
            else 
            {
                _firstChild = loc1;
                _lastChild = loc2;
            }
            arg1.cachedOrphan = true;
            setDirtyCache(true);
            return;
        }

        public insert(arg1: TweenCore, arg2=0): void
        {
            if (typeof arg2 == "string") 
            {
                if (!(arg2 in this._labels)) 
                {
                    this.addLabel(arg2, this.duration);
                }
                arg2 = Number(this._labels[arg2]);
            }
            arg1.cachedStartTime = Number(arg2) + arg1.delay;
            this.addChild(arg1);
            return;
        }

        public append(arg1: TweenCore, arg2: number=0): void
        {
            this.insert(arg1, this.duration + arg2);
            return;
        }

        public prepend(arg1: TweenCore, arg2: Boolean=false): void
        {
            this.shiftChildren(arg1.totalDuration / arg1.cachedTimeScale + arg1.delay, arg2, 0);
            this.insert(arg1, 0);
            return;
        }

        public insertMultiple(arg1: Array<any>, arg2=0, arg3: string="normal", arg4: number=0): void
        {
            var loc1=0;
            var loc2=null;
            var loc3=Number(arg2) || 0;
            var loc4=arg1.length;
            if (typeof arg2 == "string") 
            {
                if (!(arg2 in this._labels)) 
                {
                    this.addLabel(arg2, this.duration);
                }
                loc3 = this._labels[arg2];
            }
            loc1 = 0;
            while (loc1 < loc4) 
            {
                loc2 = arg1[loc1] as TweenCore;
                this.insert(loc2, loc3);
                if (arg3 != "sequence") 
                {
                    if (arg3 == "start") 
                    {
                        loc2.cachedStartTime = loc2.cachedStartTime - loc2.delay;
                    }
                }
                else 
                {
                    loc3 = loc2.cachedStartTime + loc2.totalDuration / loc2.cachedTimeScale;
                }
                loc3 = loc3 + arg4;
                ++loc1;
            }
            return;
        }

        public appendMultiple(arg1: Array<any>, arg2: number=0, arg3: string="normal", arg4: number=0): void
        {
            this.insertMultiple(arg1, this.duration + arg2, arg3, arg4);
            return;
        }

        public prependMultiple(arg1: Array<any>, arg2: string="normal", arg3: number=0, arg4: Boolean=false): void
        {
            var loc1=new TimelineLite({"tweens": arg1, "align": arg2, "stagger": arg3});
            this.shiftChildren(loc1.duration, arg4, 0);
            this.insertMultiple(arg1, 0, arg2, arg3);
            loc1.kill();
            return;
        }

        public addLabel(arg1: string, arg2: number): void
        {
            this._labels[arg1] = arg2;
            return;
        }

        public removeLabel(arg1: string): number
        {
            var loc1=this._labels[arg1];
            delete this._labels[arg1];
            return loc1;
        }

        public getLabelTime(arg1: string): number
        {
            return arg1 in this._labels ? Number(this._labels[arg1]) : -1;
        }

        protected parseTimeOrLabel(arg1): number
        {
            if (typeof arg1 == "string") 
            {
                if (!(arg1 in this._labels)) 
                {
                    throw new Error("TimelineLite error: the " + arg1 + " label was not found.");
                }
                return this.getLabelTime(String(arg1));
            }
            return Number(arg1);
        }

        public stop(): void
        {
            this.paused = true;
            return;
        }

        public gotoAndPlay(arg1, arg2: Boolean=true): void
        {
            setTotalTime(this.parseTimeOrLabel(arg1), arg2);
            play();
            return;
        }

        public gotoAndStop(arg1, arg2: Boolean=true): void
        {
            setTotalTime(this.parseTimeOrLabel(arg1), arg2);
            this.paused = true;
            return;
        }

        public goto(arg1, arg2: Boolean=true): void
        {
            setTotalTime(this.parseTimeOrLabel(arg1), arg2);
            return;
        }

        public /* override */ renderTime(arg1: number, arg2: Boolean=false, arg3: Boolean=false): void
        {
            var loc5=null;
            var loc6=false;
            var loc7=false;
            var loc8=null;
            var loc9=NaN;
            if (this.gc) 
            {
                this.setEnabled(true, false);
            }
            else if (!this.active && !this.cachedPaused) 
            {
                this.active = true;
            }
            var loc1=this.cacheIsDirty ? this.totalDuration : this.cachedTotalDuration;
            var loc2=this.cachedTime;
            var loc3=this.cachedStartTime;
            var loc4=this.cachedTimeScale;
            var loc10=this.cachedPaused;
            if (arg1 >= loc1) 
            {
                if (_rawPrevTime <= loc1 && !(_rawPrevTime == arg1)) 
                {
                    var loc11;
                    this.cachedTime = loc11 = loc1;
                    this.cachedTotalTime = loc11;
                    this.forceChildrenToEnd(loc1, arg2);
                    loc6 = !this.hasPausedChild();
                    loc7 = true;
                    if (this.cachedDuration == 0 && loc6 && (arg1 == 0 || _rawPrevTime < 0)) 
                    {
                        arg3 = true;
                    }
                }
            }
            else if (arg1 <= 0) 
            {
                if (arg1 < 0) 
                {
                    this.active = false;
                    if (this.cachedDuration == 0 && _rawPrevTime > 0) 
                    {
                        arg3 = true;
                        loc6 = true;
                    }
                }
                if (_rawPrevTime >= 0 && !(_rawPrevTime == arg1)) 
                {
                    this.forceChildrenToBeginning(0, arg2);
                    this.cachedTotalTime = 0;
                    this.cachedTime = 0;
                    loc7 = true;
                    if (this.cachedReversed) 
                    {
                        loc6 = true;
                    }
                }
            }
            else 
            {
                this.cachedTime = loc11 = arg1;
                this.cachedTotalTime = loc11;
            }
            _rawPrevTime = arg1;
            if (this.cachedTime == loc2 && !arg3) 
            {
                return;
            }
            if (!this.initted) 
            {
                this.initted = true;
            }
            if (loc2 == 0 && this.vars.onStart && !(this.cachedTime == 0) && !arg2) 
            {
                this.vars.onStart.apply(null, this.vars.onStartParams);
            }
            if (!loc7) 
            {
                if (this.cachedTime - loc2 > 0) 
                {
                    loc5 = _firstChild;
                    while (loc5) 
                    {
                        loc8 = loc5.nextNode;
                        if (this.cachedPaused && !loc10) 
                        {
                            break;
                        }
                        else if (loc5.active || !loc5.cachedPaused && loc5.cachedStartTime <= this.cachedTime && !loc5.gc) 
                        {
                            if (loc5.cachedReversed) 
                            {
                                loc9 = loc5.cacheIsDirty ? loc5.totalDuration : loc5.cachedTotalDuration;
                                loc5.renderTime(loc9 - (this.cachedTime - loc5.cachedStartTime) * loc5.cachedTimeScale, arg2, false);
                            }
                            else 
                            {
                                loc5.renderTime((this.cachedTime - loc5.cachedStartTime) * loc5.cachedTimeScale, arg2, false);
                            }
                        }
                        loc5 = loc8;
                    }
                }
                else 
                {
                    loc5 = _lastChild;
                    while (loc5) 
                    {
                        loc8 = loc5.prevNode;
                        if (!(this.cachedPaused && !loc10)) 
                        {
                        };
                        if (loc5.active || !loc5.cachedPaused && loc5.cachedStartTime <= loc2 && !loc5.gc) 
                        {
                            if (loc5.cachedReversed) 
                            {
                                loc9 = loc5.cacheIsDirty ? loc5.totalDuration : loc5.cachedTotalDuration;
                                loc5.renderTime(loc9 - (this.cachedTime - loc5.cachedStartTime) * loc5.cachedTimeScale, arg2, false);
                            }
                            else 
                            {
                                loc5.renderTime((this.cachedTime - loc5.cachedStartTime) * loc5.cachedTimeScale, arg2, false);
                            }
                        }
                        loc5 = loc8;
                    }
                }
            }
            if (_hasUpdate && !arg2) 
            {
                this.vars.onUpdate.apply(null, this.vars.onUpdateParams);
            }
            if (loc6 && (loc3 == this.cachedStartTime || !(loc4 == this.cachedTimeScale)) && (loc1 >= this.totalDuration || this.cachedTime == 0)) 
            {
                complete(true, arg2);
            }
            return;
        }

        protected forceChildrenToBeginning(arg1: number, arg2: Boolean=false): number
        {
            var loc2=null;
            var loc3=NaN;
            var loc1=_lastChild;
            var loc4=this.cachedPaused;
            while (loc1) 
            {
                loc2 = loc1.prevNode;
                if (this.cachedPaused && !loc4) 
                {
                    break;
                }
                else if (loc1.active || !loc1.cachedPaused && !loc1.gc && (!(loc1.cachedTotalTime == 0) || loc1.cachedDuration == 0)) 
                {
                    if (arg1 == 0 && (!(loc1.cachedDuration == 0) || loc1.cachedStartTime == 0)) 
                    {
                        loc1.renderTime(loc1.cachedReversed ? loc1.cachedTotalDuration : 0, arg2, false);
                    }
                    else if (loc1.cachedReversed) 
                    {
                        loc3 = loc1.cacheIsDirty ? loc1.totalDuration : loc1.cachedTotalDuration;
                        loc1.renderTime(loc3 - (arg1 - loc1.cachedStartTime) * loc1.cachedTimeScale, arg2, false);
                    }
                    else 
                    {
                        loc1.renderTime((arg1 - loc1.cachedStartTime) * loc1.cachedTimeScale, arg2, false);
                    }
                }
                loc1 = loc2;
            }
            return arg1;
        }

        protected forceChildrenToEnd(arg1: number, arg2: Boolean=false): number
        {
            var loc2=null;
            var loc3=NaN;
            var loc1=_firstChild;
            var loc4=this.cachedPaused;
            while (loc1) 
            {
                loc2 = loc1.nextNode;
                if (this.cachedPaused && !loc4) 
                {
                    break;
                }
                else if (loc1.active || !loc1.cachedPaused && !loc1.gc && (!(loc1.cachedTotalTime == loc1.cachedTotalDuration) || loc1.cachedDuration == 0)) 
                {
                    if (arg1 == this.cachedDuration && (!(loc1.cachedDuration == 0) || loc1.cachedStartTime == this.cachedDuration)) 
                    {
                        loc1.renderTime(loc1.cachedReversed ? 0 : loc1.cachedTotalDuration, arg2, false);
                    }
                    else if (loc1.cachedReversed) 
                    {
                        loc3 = loc1.cacheIsDirty ? loc1.totalDuration : loc1.cachedTotalDuration;
                        loc1.renderTime(loc3 - (arg1 - loc1.cachedStartTime) * loc1.cachedTimeScale, arg2, false);
                    }
                    else 
                    {
                        loc1.renderTime((arg1 - loc1.cachedStartTime) * loc1.cachedTimeScale, arg2, false);
                    }
                }
                loc1 = loc2;
            }
            return arg1;
        }

        public hasPausedChild(): Boolean
        {
            var loc1=this.gc ? this._endCaps[0] : _firstChild;
            while (loc1) 
            {
                if (loc1.cachedPaused || loc1 is TimelineLite && (loc1 as TimelineLite).hasPausedChild()) 
                {
                    return true;
                }
                loc1 = loc1.nextNode;
            }
            return false;
        }

        public getChildren(arg1: Boolean=true, arg2: Boolean=true, arg3: Boolean=true, arg4: number=-9999999999): Array<any>
        {
            var loc1=[];
            var loc2=0;
            var loc3=this.gc ? this._endCaps[0] : _firstChild;
            while (loc3) 
            {
                if (!(loc3.cachedStartTime < arg4)) 
                {
                    if (typeof loc3 === "tweenlite") 
                    {
                        if (arg2) 
                        {
                            var loc4;
                            loc1[loc4 = loc2++] = loc3;
                        }
                    }
                    else 
                    {
                        if (arg3) 
                        {
                            loc1[loc4 = loc2++] = loc3;
                        }
                        if (arg1) 
                        {
                            loc1 = loc1.concat(TimelineLite(loc3).getChildren(true, arg2, arg3));
                        }
                    }
                }
                loc3 = loc3.nextNode;
            }
            return loc1;
        }

        public getTweensOf(arg1: Object, arg2: Boolean=true): Array<any>
        {
            var loc3=0;
            var loc1=this.getChildren(arg2, true, false);
            var loc2=[];
            var loc4=loc1.length;
            var loc5=0;
            loc3 = 0;
            while (loc3 < loc4) 
            {
                if (TweenLite(loc1[loc3]).target == arg1) 
                {
                    var loc6;
                    loc2[loc6 = loc5++] = loc1[loc3];
                }
                ++loc3;
            }
            return loc2;
        }

        public shiftChildren(arg1: number, arg2: Boolean=false, arg3: number=0): void
        {
            var loc2=null;
            var loc1=this.gc ? this._endCaps[0] : _firstChild;
            while (loc1) 
            {
                if (loc1.cachedStartTime >= arg3) 
                {
                    loc1.cachedStartTime = loc1.cachedStartTime + arg1;
                }
                loc1 = loc1.nextNode;
            }
            if (arg2) 
            {
                var loc3=0;
                var loc4=this._labels;
                for (loc2 in loc4) 
                {
                    if (!(this._labels[loc2] >= arg3)) 
                    {
                        continue;
                    }
                    this._labels[loc2] = this._labels[loc2] + arg1;
                }
            }
            this.setDirtyCache(true);
            return;
        }

        public killTweensOf(arg1: Object, arg2: Boolean=true, arg3: Object=null): Boolean
        {
            var loc3=null;
            var loc1;
            var loc2=(loc1 = this.getTweensOf(arg1, arg2)).length;
            while (--loc2 > -1) 
            {
                loc3 = loc1[loc2];
                if (arg3 != null) 
                {
                    loc3.killVars(arg3);
                }
                if (!(arg3 == null || loc3.cachedPT1 == null && loc3.initted)) 
                {
                    continue;
                }
                loc3.setEnabled(false, false);
            }
            return Boolean(loc1.length > 0);
        }

        public /* override */ invalidate(): void
        {
            var loc1=this.gc ? this._endCaps[0] : _firstChild;
            while (loc1) 
            {
                loc1.invalidate();
                loc1 = loc1.nextNode;
            }
            return;
        }

        public clear(arg1: Array<any>=null): void
        {
            if (arg1 == null) 
            {
                arg1 = this.getChildren(false, true, true);
            }
            var loc1=arg1.length;
            while (--loc1 > -1) 
            {
                TweenCore(arg1[loc1]).setEnabled(false, false);
            }
            return;
        }

        public /* override */ setEnabled(arg1: Boolean, arg2: Boolean=false): Boolean
        {
            var loc1=null;
            var loc2=null;
            if (arg1 == this.gc) 
            {
                if (arg1) 
                {
                    var loc3;
                    loc1 = loc3 = this._endCaps[0];
                    _firstChild = loc3;
                    _lastChild = this._endCaps[1];
                    this._endCaps = [null, null];
                }
                else 
                {
                    loc1 = _firstChild;
                    this._endCaps = [_firstChild, _lastChild];
                    _lastChild = loc3 = null;
                    _firstChild = loc3;
                }
                while (loc1) 
                {
                    loc1.setEnabled(arg1, true);
                    loc1 = loc1.nextNode;
                }
            }
            return super.setEnabled(arg1, arg2);
        }

        public get currentProgress(): number
        {
            return this.cachedTime / this.duration;
        }

        public set currentProgress(arg1: number)
        {
            setTotalTime(this.duration * arg1, false);
            return;
        }

        public /* override */ get duration(): number
        {
            var loc1=NaN;
            if (this.cacheIsDirty) 
            {
                loc1 = this.totalDuration;
            }
            return this.cachedDuration;
        }

        public /* override */ set duration(arg1: number)
        {
            if (!(this.duration == 0) && !(arg1 == 0)) 
            {
                this.timeScale = this.duration / arg1;
            }
            return;
        }

        public /* override */ get totalDuration(): number
        {
            var loc1=NaN;
            var loc2=NaN;
            var loc3=null;
            var loc4=NaN;
            var loc5=null;
            if (this.cacheIsDirty) 
            {
                loc1 = 0;
                loc3 = this.gc ? this._endCaps[0] : _firstChild;
                loc4 = -Infinity;
                while (loc3) 
                {
                    loc5 = loc3.nextNode;
                    if (loc3.cachedStartTime < loc4) 
                    {
                        this.addChild(loc3);
                        loc4 = loc3.prevNode.cachedStartTime;
                    }
                    else 
                    {
                        loc4 = loc3.cachedStartTime;
                    }
                    if (loc3.cachedStartTime < 0) 
                    {
                        loc1 = loc1 - loc3.cachedStartTime;
                        this.shiftChildren(-loc3.cachedStartTime, false, -9999999999);
                    }
                    loc2 = loc3.cachedStartTime + loc3.totalDuration / loc3.cachedTimeScale;
                    if (loc2 > loc1) 
                    {
                        loc1 = loc2;
                    }
                    loc3 = loc5;
                }
                var loc6;
                this.cachedTotalDuration = loc6 = loc1;
                this.cachedDuration = loc6;
                this.cacheIsDirty = false;
            }
            return this.cachedTotalDuration;
        }

        public /* override */ set totalDuration(arg1: number)
        {
            if (!(this.totalDuration == 0) && !(arg1 == 0)) 
            {
                this.timeScale = this.totalDuration / arg1;
            }
            return;
        }

        public get timeScale(): number
        {
            return this.cachedTimeScale;
        }

        public set timeScale(arg1: number)
        {
            if (arg1 == 0) 
            {
                arg1 = 0.0001;
            }
            var loc1=_pauseTime || _pauseTime == 0 ? _pauseTime : this.timeline.cachedTotalTime;
            this.cachedStartTime = loc1 - (loc1 - this.cachedStartTime) * this.cachedTimeScale / arg1;
            this.cachedTimeScale = arg1;
            setDirtyCache(false);
            return;
        }

        public get useFrames(): Boolean
        {
            var loc1=this.timeline;
            while (loc1.timeline) 
            {
                loc1 = loc1.timeline;
            }
            return Boolean(loc1 == TweenLite.rootFramesTimeline);
        }

        public static /* const */version: number=1.382;

        protected /* var */_labels: Object;

        protected /* var */_endCaps: Array<any>;

        private static /* var */_overwriteMode: int;
    }
